(function (vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addShopCommunityInfo: {
                scId: '',
                communityId: '',
                communityName: '',
                cityCode: '',
                areas: [],
                provs: [],
                citys: [],
                selectProv: '',
                selectCity: '',
                selectArea: '',
                address: '',
                cityCode: '',
                codeName: '',
                shopId: vc.getCurrentCommunity().shopId,
                communitys: []
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('addShopCommunity', 'openAddShopCommunityModal', function () {
                $that._initArea('101', '0');
                $('#addShopCommunityModel').modal('show');
            });
        },
        methods: {
            addShopCommunityValidate() {
                return vc.validate.validate({
                    addShopCommunityInfo: $that.addShopCommunityInfo
                }, {
                    'addShopCommunityInfo.communityId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "小区不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "小区错误"
                        },
                    ],
                });
            },
            saveShopCommunityInfo: function () {
                if (!$that.addShopCommunityValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addShopCommunityInfo);
                    $('#addShopCommunityModel').modal('hide');
                    return;
                }

                vc.http.apiPost(
                    '/shop.saveShopCommunity',
                    JSON.stringify($that.addShopCommunityInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopCommunityModel').modal('hide');
                            $that.clearAddShopCommunityInfo();
                            vc.emit('shopCommunityManage', 'listShopCommunity', {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddShopCommunityInfo: function () {
                $that.addShopCommunityInfo = {
                    scId: '',
                    communityId: '',
                    communityName: '',
                    cityCode: '',
                    areas: [],
                    provs: [],
                    citys: [],
                    selectProv: '',
                    selectCity: '',
                    selectArea: '',
                    address: '',
                    cityCode: '',
                    codeName: '',
                    shopId: vc.getCurrentCommunity().shopId,
                    communitys: []
                };
            },
            getProv: function (_prov) {
                $that._initArea('202', _prov);
            },
            getCity: function (_city) {
                $that._initArea('303', _city);
            },
            getArea: function (_area) {
                $that.addShopCommunityInfo.cityCode = _area;
                let _param = {
                    params: {
                        shopId: vc.getCurrentCommunity().shopId,
                        page: 1,
                        row: 500,
                        areaCode: _area
                    }
                }
                //查询系统中的小区
                vc.http.apiGet('/shop.queryShopFreeCommunity',
                    _param,
                    function (json, res) {
                        if (res.status == 200) {
                            var _tmpAreas = JSON.parse(json);
                            $that.addShopCommunityInfo.communitys = _tmpAreas.data;
                            return;
                        }
                        //$that.$emit('errorInfoEvent',json);
                    }, function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                        vc.toast("查询地区失败");
                    }
                );
            },
            _changeCommunity: function () {
                if (!$that.addShopCommunityInfo.communityId) {
                    return;
                }
                $that.addShopCommunityInfo.communitys.forEach(item => {
                    if (item.communityId == $that.addShopCommunityInfo.communityId) {
                        $that.addShopCommunityInfo.communityName = item.name;
                        $that.addShopCommunityInfo.address = item.address;
                        $that.addShopCommunityInfo.cityCode = item.areaCode;
                        $that.addShopCommunityInfo.codeName = item.areaName;
                    }
                });
            },
            _initArea: function (_areaLevel, _parentAreaCode) { //加载区域
                var _param = {
                    params: {
                        areaLevel: _areaLevel,
                        parentAreaCode: _parentAreaCode
                    }
                };
                vc.http.apiGet('/area.listAreas',
                    _param,
                    function (json, res) {
                        if (res.status == 200) {
                            var _tmpAreas = JSON.parse(json).data;
                            if (_areaLevel == '101') {
                                $that.addShopCommunityInfo.provs = _tmpAreas;
                            } else if (_areaLevel == '202') {
                                $that.addShopCommunityInfo.citys = _tmpAreas;
                            } else {
                                $that.addShopCommunityInfo.areas = _tmpAreas;
                            }
                            return;
                        }
                        //$that.$emit('errorInfoEvent',json);
                    }, function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                        vc.toast("查询地区失败");
                    });
            },
        }
    });
})(window.vc);
